package com.advertisementsystem.Mapper;

import com.advertisementsystem.entity.Permission;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PermissionMapper extends BaseMapper<Permission> {
    @Select("SELECT DISTINCT p.* FROM permission p " +
            "LEFT JOIN role_permission rp ON p.id = rp.permission_id " +
            "LEFT JOIN user_role ur ON rp.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId} " +
            "AND p.status = 1 " +
            "ORDER BY p.sort_order")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "code", property = "code"),
            @Result(column = "description", property = "description"),
            @Result(column = "type", property = "type"),
            @Result(column = "parent_id", property = "parentId"),
            @Result(column = "path", property = "path"),
            @Result(column = "component", property = "component"),
            @Result(column = "icon", property = "icon"),
            @Result(column = "sort_order", property = "sortOrder"),
            @Result(column = "status", property = "status"),
            @Result(column = "created_time", property = "createdTime"),
            @Result(column = "updated_time", property = "updatedTime")
    })
    List<Permission> selectPermissionsByUserId(Long userId);

    @Select("SELECT p.* FROM permission p " +
            "JOIN role_permission rp ON p.id = rp.permission_id " +
            "WHERE rp.role_id = #{roleId} " +
            "ORDER BY p.sort_order")
    List<Permission> selectPermissionsByRoleId(Long roleId);

    @Select("SELECT p.id FROM permission p " +
            "JOIN role_permission rp ON p.id = rp.permission_id " +
            "WHERE rp.role_id = #{roleId}")
    List<Long> selectPermissionIdsByRoleId(Long roleId);
}